home *** CD-ROM | disk | FTP | other *** search
- VERSION 4.00
- Begin VB.Form BounceForm
- Caption = "Bounce1"
- ClientHeight = 5235
- ClientLeft = 1320
- ClientTop = 1110
- ClientWidth = 6870
- Height = 5925
- Left = 1260
- LinkTopic = "Form1"
- ScaleHeight = 349
- ScaleMode = 3 'Pixel
- ScaleWidth = 458
- Top = 480
- Width = 6990
- Begin VB.TextBox FPSText
- Height = 285
- Left = 1440
- TabIndex = 4
- Text = "20"
- Top = 4920
- Width = 375
- End
- Begin VB.TextBox BallsText
- Height = 285
- Left = 1440
- TabIndex = 3
- Text = "20"
- Top = 4560
- Width = 375
- End
- Begin VB.CommandButton CmdStart
- Caption = "Start"
- Default = -1 'True
- Height = 495
- Left = 2160
- TabIndex = 1
- Top = 4620
- Width = 855
- End
- Begin VB.PictureBox Court
- AutoRedraw = -1 'True
- Height = 4455
- Left = 0
- ScaleHeight = 293
- ScaleMode = 3 'Pixel
- ScaleWidth = 453
- TabIndex = 0
- Top = 0
- Width = 6855
- End
- Begin VB.Label Label1
- Caption = "Frames per second:"
- Height = 255
- Index = 0
- Left = 0
- TabIndex = 5
- Top = 4920
- Width = 1455
- End
- Begin VB.Label Label1
- Caption = "Number of balls:"
- Height = 255
- Index = 1
- Left = 0
- TabIndex = 2
- Top = 4560
- Width = 1455
- End
- Begin VB.Menu mnuFile
- Caption = "&File"
- Begin VB.Menu mnuFileExit
- Caption = "E&xit"
- End
- End
- Attribute VB_Name = "BounceForm"
- Attribute VB_Creatable = False
- Attribute VB_Exposed = False
- Option Explicit
- Dim xmax As Integer
- Dim ymax As Integer
- Dim NumBalls As Integer
- Dim BallR() As Integer
- Dim BallX() As Integer
- Dim BallY() As Integer
- Dim BallDx() As Integer
- Dim BallDy() As Integer
- Dim BallClr() As Long
- Dim Playing As Boolean
- ' ************************************************
- ' Generate some random data.
- ' ************************************************
- Sub InitData()
- Dim ball As Integer
- Dim R As Integer
- Dim clr As Integer
- ' See how many balls there should be.
- If Not IsNumeric(BallsText.Text) Then _
- BallsText.Text = "10"
- NumBalls = CInt(BallsText.Text)
- ReDim BallR(1 To NumBalls)
- ReDim BallX(1 To NumBalls)
- ReDim BallY(1 To NumBalls)
- ReDim BallDx(1 To NumBalls)
- ReDim BallDy(1 To NumBalls)
- ReDim BallClr(1 To NumBalls)
- ' Set the initial ball data.
- For ball = 1 To NumBalls
- R = Int(10 * Rnd + 5)
- BallR(ball) = R
- BallX(ball) = Int((xmax - R + 1) * Rnd)
- BallY(ball) = Int((ymax - R + 1) * Rnd)
- BallDx(ball) = Int(21 * Rnd - 10)
- BallDy(ball) = Int(21 * Rnd - 10)
- clr = Int(15 * Rnd)
- If clr >= 7 Then clr = clr + 1
- BallClr(ball) = QBColor(clr)
- Next ball
- End Sub
- ' ************************************************
- ' Start the animation.
- ' ************************************************
- Private Sub CmdStart_Click()
- If Playing Then
- Playing = False
- CmdStart.Caption = "Stopped"
- CmdStart.Enabled = False
- Else
- CmdStart.Caption = "Stop"
- Playing = True
- InitData
- PlayData
- Playing = False
- CmdStart.Caption = "Start"
- CmdStart.Enabled = True
- End If
- End Sub
- ' ************************************************
- ' Play the animation.
- ' ************************************************
- Sub PlayData()
- Dim mpf As Long ' Milliseconds per frame.
- Dim ball As Integer
- Dim next_time As Long
- Dim old_style As Integer
- Dim frames As Integer
- Dim start_time As Single
- Dim stop_time As Single
- ' Set FillStyle to vbSolid.
- old_style = Court.FillStyle
- Court.FillStyle = vbSolid
- ' See how fast we should go.
- If Not IsNumeric(FPSText.Text) Then _
- FPSText.Text = "10"
- mpf = 1000 \ CLng(FPSText.Text)
- ' Start the animation.
- start_time = Timer
- next_time = GetTickCount()
- Do While Playing
- frames = frames + 1
-
- ' Draw the balls.
- Court.Cls
- For ball = 1 To NumBalls
- Court.FillColor = BallClr(ball)
- Court.Circle _
- (BallX(ball), BallY(ball)), _
- BallR(ball), BallClr(ball)
- Next ball
-
- ' Move the balls.
- For ball = 1 To NumBalls
- BallX(ball) = BallX(ball) + BallDx(ball)
- If BallX(ball) < BallR(ball) Then
- BallX(ball) = 2 * BallR(ball) - BallX(ball)
- BallDx(ball) = -BallDx(ball)
- ElseIf BallX(ball) > xmax - BallR(ball) Then
- BallX(ball) = 2 * (xmax - BallR(ball)) - BallX(ball)
- BallDx(ball) = -BallDx(ball)
- End If
-
- BallY(ball) = BallY(ball) + BallDy(ball)
- If BallY(ball) < BallR(ball) Then
- BallY(ball) = 2 * BallR(ball) - BallY(ball)
- BallDy(ball) = -BallDy(ball)
- ElseIf BallY(ball) > ymax - BallR(ball) Then
- BallY(ball) = 2 * (ymax - BallR(ball)) - BallY(ball)
- BallDy(ball) = -BallDy(ball)
- End If
- Next ball
-
- ' Wait until it's time for the next frame.
- next_time = next_time + mpf
- WaitTill next_time
- Loop
- stop_time = Timer
- MsgBox "Displayed" & Str$(frames) & _
- " frames in " & _
- Format$(stop_time - start_time, "0.00") & _
- " seconds (" & _
- Format$(frames / (stop_time - start_time), "0.00") & _
- " FPS)."
- ' Restore the old FillStyle.
- Court.FillStyle = old_style
- End Sub
- ' ************************************************
- ' Make the ball court nice and big.
- ' ************************************************
- Private Sub Form_Resize()
- Const GAP = 3
- FPSText.Top = ScaleHeight - GAP - FPSText.Height
- Label1(0).Top = FPSText.Top
- BallsText.Top = FPSText.Top - GAP - BallsText.Height
- Label1(1).Top = BallsText.Top
- CmdStart.Top = (BallsText.Top + FPSText.Top + FPSText.Height - CmdStart.Height) / 2
- Court.Move 0, 0, ScaleWidth, BallsText.Top - GAP
- xmax = Court.ScaleWidth - 1
- ymax = Court.ScaleHeight - 1
- End Sub
- Private Sub Form_Unload(Cancel As Integer)
- End
- End Sub
- Private Sub mnuFileExit_Click()
- Unload Me
- End Sub
-